﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MOJsdk;

namespace MOJ
{
    /// <summary>
    /// Interaction logic for Control_FolderSettings.xaml
    /// </summary>
    public partial class Control_FolderSettings : UserControl
    {
        private IHost _host;

        public Control_FolderSettings()
        {
            InitializeComponent();
            _host = GlobalVars.TheHost;
            INI();
        }


        private void INI()
        {
            INIEvents();
            INIStyles();
            LoadData();
        }
        private void INIEvents()
        {
            this.Loaded += delegate { LoadData(); };
            _btnSave.Click += delegate { SaveData(); };

            _btnImages.Click += LocateFolder;
            _btnLang.Click += LocateFolder;
            _btnLog.Click += LocateFolder;
            _btnPlugin.Click += LocateFolder;
            _btnSkin.Click += LocateFolder;
            _btnTemp.Click += LocateFolder;
        }
        private void INIStyles()
        {
            _lblLang.ApplyStyle("0101066");
            _lblLang.Content = MOJLocalization.Localize("Languages") + ": ";
            _txtBoxLang.ApplyStyle("0101067");
            _btnLang.ApplyStyle("0101068");

            _lblPlug.ApplyStyle("0101069");
            _lblPlug.Content = MOJLocalization.Localize("Plugins") + ": ";
            _txtBoxPlug.ApplyStyle("0101070");
            _btnPlugin.ApplyStyle("0101071");

            _lblSkin.ApplyStyle("0101072");
            _lblSkin.Content = MOJLocalization.Localize("Skin") + ": ";
            _txtBoxSkin.ApplyStyle("0101073");
            _btnSkin.ApplyStyle("0101074");

            _lblImages.ApplyStyle("0101075");
            _lblImages.Content = MOJLocalization.Localize("Images") + ": ";
            _txtBoxImages.ApplyStyle("0101076");
            _btnImages.ApplyStyle("0101077");

            _lblLog.ApplyStyle("0101078");
            _lblLog.Content = MOJLocalization.Localize("Logs") + ": ";
            _txtboxLogs.ApplyStyle("0101079");
            _btnLog.ApplyStyle("0101080");

            _lblTemp.ApplyStyle("0101081");
            _lblTemp.Content = MOJLocalization.Localize("Temp") + ": ";
            _txtBoxTemp.ApplyStyle("0101082");
            _btnTemp.ApplyStyle("0101083");
            _btnSave.ApplyStyle("0101084");
            _btnSave.Content = MOJLocalization.Localize("Save");
        }
        private void LoadData()
        {
            MojSystem _sys = _host.SystemVars;

            _txtBoxImages.Text = _sys.ImagesFolder;
            _txtBoxLang.Text = _sys.LanguageFolder;
            _txtboxLogs.Text = _sys.LogFolder;
            _txtBoxPlug.Text = _sys.PluginFolders;
            _txtBoxSkin.Text = _sys.StyleFolder;
            _txtBoxTemp.Text = _sys.TempFolder;
        }

        private void SaveData()
        {
            MojSystem _sys = _host.SystemVars;

            _sys.SetVar("MOJLanguageFolder", _txtBoxLang.Text);
            _sys.SetVar("MOJPluginFolder", _txtBoxPlug.Text);
            _sys.SetVar("MOJSkinFolder", _txtBoxSkin.Text);
            _sys.SetVar("MOJTempFolder", _txtBoxTemp.Text);
            _sys.SetVar("MOJImagesFolder", _txtBoxImages.Text);
            _sys.SetVar("MOJLogFolder", _txtboxLogs.Text);
        }

        private void LocateFolder(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            switch (btn.Name)
            {
                case "_btnImages":
                    _txtBoxImages.Text = GetFolder(_txtBoxImages.Text);
                    break;

                case "_btnLang":
                    _txtBoxLang.Text = GetFolder(_txtBoxLang.Text);
                    break;
                    
                case "_btnLog":
                    _txtboxLogs.Text = GetFolder(_txtboxLogs.Text);
                    break;

                case "_btnPlugin":
                    _txtBoxPlug.Text = GetFolder(_txtBoxPlug.Text);
                    break;

                case "_btnSkin":
                    _txtBoxSkin.Text = GetFolder(_txtBoxSkin.Text);
                    break;

                case "_btnTemp":
                    _txtBoxTemp.Text = GetFolder(_txtBoxTemp.Text);
                    break;

                default:
                    break;
            }
        }

        private string GetFolder(string StartPoint)
        {
            System.Windows.Forms.FolderBrowserDialog diag = new System.Windows.Forms.FolderBrowserDialog();
            diag.Description = MOJLocalization.Localize("Select Folder");

            if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                return diag.SelectedPath;

            return StartPoint;
        }
    }
}
